<?php
date_default_timezone_set("Asia/Shanghai");
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
$conn_args = array(
    'host' => 'localhost',
    'port' => 5672,
    'login' => 'admin',
    'password' => 'admin2021',
    'vhost' => 'why'
);
 
$ex_name = 'test_exchange';
$queue_name = 'queue1';
$route = 'why.*';
 
$conn = new AMQPConnection($conn_args);
if(!$conn->connect())
{
    die("connect error");
}
 
$channel = new AMQPChannel($conn);
 
$exchange = new AMQPExchange($channel);
$exchange->setName($ex_name);
$exchange->setType('topic');
$exchange->declare();
 
 
$queue = new AMQPQueue($channel);
$queue->setName($queue_name);
$queue->setFlags(AMQP_DURABLE);
$queue->declare();
$queue->bind($ex_name, $route);

echo "Message:\n";   
while(True){ 
    $queue->consume('processMessage');   
    //$queue->consume('processMessage', AMQP_AUTOACK); //自动ACK应答  
} 
$conn->disconnect();   
 
function processMessage($envelope, $queue) { 
    $msg = $envelope->getBody(); 
    echo $msg."\n";
    $queue->ack($envelope->getDeliveryTag());
}